package com.leesche.zcsp.node.web.controller.web.operation;

import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevicetype;
import com.leesche.zcsp.node.web.service.PubDevicetypeService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDevicetypeDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceTypeCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by kaider on 2016/9/29.
 */
@Controller
@RequestMapping("/web/deviceType")
public class DeviceTypeController
{
    private final static Logger LOGGER = LoggerFactory.getLogger(DeviceTypeController.class);

    private static final String LIST_VIEW = "web/deviceType/listView";
    private static final String ADD_VIEW = "web/deviceType/addView";
    private static final String EDIT_VIEW = "web/deviceType/editView";

    @Autowired
    private PubDevicetypeService deviceTypeService;

    @Autowired
    private PrivilegeService privilegeService;

    @RequestMapping(value = "/listView", method = RequestMethod.GET)
    public String listView()
    {
        return LIST_VIEW;
    }

    @ResponseBody
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    public Map<String, Object> listJson(
            final QueryPubDeviceTypeCondition condition
    )
    {
        PagingResult<PubDevicetypeDto> pagingResult = deviceTypeService.findAllByPage(condition);
        Map<String, Object> json = WebResult.buildOf(pagingResult).toMap();
        return json;
    }

    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    public String addView()
    {
        return ADD_VIEW;
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String, Object> add(
            final PubDevicetype devicetype
    )
    {
        try {
            devicetype.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            deviceTypeService.insert(devicetype);
            return WebResult.success().info("操作成功").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    @RequestMapping(value = "/{id}/editView", method = RequestMethod.GET)
    public String editView(
            final @PathVariable("id") Long id,
            final Model model
    )
    {
        PubDevicetype devicetype = deviceTypeService.selectByPrimaryKey(id);
        model.addAttribute("devicetype", devicetype);
        return EDIT_VIEW;
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Map<String, Object> edit(
            final PubDevicetype devicetype
    )
    {
        try {
            deviceTypeService.updateByPrimaryKeySelective(devicetype);
            return WebResult.success().info("操作成功").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public Map<String, Object> delete(
            final @PathVariable("id") Long id
    )
    {
        try {
            deviceTypeService.deleteByPrimaryKey(id);
            return WebResult.success().info("操作成功").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

}
